﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Explorer.cs" company="Anton Frattaroli">
//   Copyright (c) Anton Frattaroli. All rights reserved.
// </copyright>
// <summary>
//   Defines the Explorer class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Lidocaine.Data
{
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using Lidocaine.BusinessObjects;
    
    /// <summary>
    /// Defines the Explorer class.
    /// </summary>
    public static class Explorer
    {
        /// <summary>
        /// Gets the folders, pages and files for this application.
        /// </summary>
        /// <param name="path">The current path to query for pages, files and folders.</param>
        /// <param name="depth">The depth of the current path.</param>
        /// <returns>An array of explorer items.</returns>
        public static List<BusinessObjects.ExplorerItem> SelectAll(string path, string depth)
        {
            BusinessObjects.ExplorerItem item = null;
            List<BusinessObjects.ExplorerItem> items = new List<BusinessObjects.ExplorerItem>();
            using (SqlConnection conn = new SqlConnection(Environment.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.LoadFiles", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    if (!string.IsNullOrEmpty(path))
                    {
                        cmd.CommandText = "Lidocaine.LoadFiles2";
                        cmd.Parameters.AddWithValue("@Address", path);
                        cmd.Parameters.AddWithValue("@FolderCount", depth);
                    }

                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        if (rdr.HasRows)
                        {
                            while (rdr.Read())
                            {
                                item = new BusinessObjects.ExplorerItem();
                                item.Source = (Source)rdr["Source"];
                                item.Path = rdr["Path"].ToString();
                                items.Add(item);
                            }
                        }
                    }
                }
            }

            return items;
        }
    }
}